<script>
import { reactive } from "@vue/reactivity";
import { useRouter } from "vue-router";
import { public_status } from "../store/public_status";
import { useStore } from "../store/index";
import { Toast } from "vant";
export default {
  setup() {
    let data = reactive({
      sumlist: [300, 500, 1000, 3000, 5000, 10000, 30000, 50000, 100000],
      active: null,
      sum: "",

      isRecharge: false,
    });

    public_status().display = false;

    //  # 路由配置
    let router = useRouter();

    // # 用户数据
    let userId = localStorage.getItem("P4_userId");
    let userData = useStore().userData[userId];

    // # 点击切换
    let change = (i, item) => {
      data.active = i;
      data.sum = item;
    };

    // # 充值按钮
    let recharge = () => {
      let recharge_num = Number(data.sum);

      if (recharge_num) {
        Toast.loading({
          message: "充值中...",
          forbidClick: true,
          loadingType: "spinner",
          onClose: () => {
            userData.balance += recharge_num;
            Toast.success("充值成功");
            router.back();
          },
        });
      } else {
        Toast({
          message: "请输入大于0的金额",
          position: "bottom",
        });
      }

      data.isRecharge = false;
    };

    return {
      data,
      router,

      change,
      recharge,
    };
  },
};
</script>

<template>
  <div class="rechargeView">
    <!-- 头部 -->
    <header class="header">
      <div class="back" @click="router.back()">
        <i class="iconfont icon-arrow-left"></i>
      </div>
      <div class="title">Recharge</div>
    </header>
    <!-- 头部 -->

    <!-- 充值输入 -->
    <div class="amountbox">
      <div class="top">
        <div class="title">充值金额</div>
        <div class="amount">
          <span>￥</span
          ><input type="text" v-model="data.sum" placeholder="请输入金额" />
        </div>
      </div>
      <div class="line"></div>
    </div>
    <!-- 充值选择 -->
    <div class="explain">充值说明</div>

    <ul class="buy_box">
      <li
        v-for="(item, index) in data.sumlist"
        :key="index"
        :class="{ con: data.active == index }"
        @click="change(index, item)"
      >
        {{ item }}元
      </li>
    </ul>

    <div class="btn" @click="data.isRecharge = true">下一步</div>

    <!-- 确认充值弹框 -->
    <van-popup
      class="isGoPay"
      :style="{ 'border-radius': '10px' }"
      v-model:show="data.isRecharge"
      :overlay-style="{ background: 'rgba(0,0,0,0.6)' }"
    >
      <div class="content">
        <div class="question">是否充值？</div>
        <div class="bottom">
          <div class="item" @click="data.isRecharge = false">否</div>
          <div class="item" @click="recharge">是</div>
        </div>
      </div>
    </van-popup>
  </div>
</template>

<style lang="scss" scoped>
.rechargeView {
  > header.header {
    height: 56px;
    line-height: 56px;
    text-align: center;

    background: #333;

    position: sticky;
    top: 0;
    z-index: 10;
    color: #fff;

    > .back {
      position: absolute;
      left: 15px;
      top: 0;
      bottom: 0;
      margin: auto;

      width: 37px;
      height: 37px;
      line-height: 37px;
      border-radius: 50%;
      background: #7b7979;

      > i.icon-arrow-left {
        text-align: center;
        color: #fff;
      }
    }

    > .title {
      font-size: 18px;
      font-weight: bold;
    }
  }

  //   充值金额
  .amountbox {
    padding: 0 16px;
    height: 80px;
    background-color: #333;
    color: #fff;
    font-weight: 600px;

    .top {
      height: 60px;
      display: flex;
      align-items: center;
      justify-content: space-between;
      //   border-bottom: 3px solid rgb(98, 97, 97);
      .title {
        font-size: 12px;
        font-weight: 700;
      }

      .amount {
        display: flex;
      }

      span {
        font-size: 22px;
        font-weight: 700;
        margin-right: 5px;
      }

      input {
        height: 30px;
        line-height: 20px;
        background-color: #333;
        color: #fff;
        border: transparent;
        font-size: 16px;
        font-weight: 600;
        width: 100px;
      }

      input::-webkit-input-placeholder {
        color: #fff;

        font-size: 16px;
        font-weight: 600;
      }
    }
    .line {
      width: 100%;
      height: 2px;
      background: linear-gradient(
        244deg,
        rgba(255, 255, 255, 0) 0%,
        rgb(94, 92, 92) 50%,
        rgba(255, 255, 255, 0) 100%
      );
    }
  }

  //   充值选择
  .explain {
    font-size: 12px;
    padding: 0 15px;
    height: 50px;
    line-height: 50px;
    color: #8f8f8f;
    font-weight: 700;
  }

  .buy_box {
    background-color: #fff;
    padding: 0 15px;
    display: flex;
    flex-wrap: wrap;
    justify-content: space-between;
    color: #333333;
    font-weight: 700;

    li {
      width: 100px;
      height: 60px;
      line-height: 60px;
      border: 1px solid #b7b3b3;
      border-radius: 10px;
      text-align: center;
      margin-bottom: 10px;
      background-color: #f8f8fa;

      &.con {
        background-color: #444;
        color: #fff;
      }
    }
  }

  > .btn {
    margin: 50px 35px;

    height: 50px;
    line-height: 50px;
    background-color: #333;
    color: #fff;
    font-weight: 700;
    text-align: center;
    border-radius: 10px;
  }

  > .isGoPay {
    position: relative;

    > .content {
      width: 230px;
      height: 120px;
      overflow: hidden;

      > .question {
        height: 80px;
        line-height: 80px;
        font-size: 14px;
        font-weight: bold;
        text-align: center;
        border-bottom: 1px solid #e8e8e8;
      }

      > .bottom {
        display: flex;
        height: 40px;
        line-height: 40px;

        > .item {
          flex: 1;
          text-align: center;
          border-right: 1px solid #e8e8e8;
        }
      }
    }
  }
}
</style>